﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;

namespace caufingHealth
{
    class InformationRetriever : IData
    {
        private OdbcConnection caufingDbConnection;
        public InformationRetriever()
        {
            string conStr = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=cs.westga.edu; PORT=3307; DATABASE=cs3230f11a; UID=cs3230f11a; PASSWORD=HdPYWx7bRE4rhBTM;";
          //  string conStr = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=www.officialbunny.com; PORT=3306; DATABASE=cs3230f11a; UID=info; PASSWORD=info;";
            caufingDbConnection = new OdbcConnection(conStr);
        }
        
        public List<KeyValuePair<string, object>>[] Search(string firstName, string lastName)
        {
            return GetResults("SELECT * FROM `Patient` WHERE FirstName='" + firstName + "' AND LastName='" + lastName + "'");
        }

        public List<KeyValuePair<string, object>>[] Search(DateTime dateOfBirth)
        {
            return GetResults("SELECT * FROM Patient WHERE DateofBirth=" + dateOfBirth.Ticks + ";");
        }

        public List<KeyValuePair<string, object>>[] Search(string firstName, string lastName, DateTime dateOfBirth)
        {
            return GetResults("SELECT * FROM `Patient` WHERE FirstName='" + firstName + "' AND LastName='" + lastName + "' AND DateofBirth='" + dateOfBirth.Ticks + ";");
        }

        public List<KeyValuePair<string, object>>[] Search(string lastname, DateTime dateOfBirth)
        {
            return GetResults("SELECT * FROM Patient WHERE LastName='" + lastname + "' AND DateofBirth=" + dateOfBirth.Ticks + ";");
        }
        
        public bool AddPatientRecord(PatientInformation.PatientRecord pinfo)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("INSERT INTO Patient(FirstName, LastName, `Street#`, StreetName, City, `State`, ZipCode, DateOfBirth, `Contact#`) VALUES ('" + 
                                                                              pinfo.Name.FirstName + "','" +
                                                                              pinfo.Name.LastName + "'," +
                                                                              int.Parse(pinfo.Address.Address.Substring(0,pinfo.Address.Address.IndexOf(" ")).Trim()) + ",'" +
                                                                              pinfo.Address.Address.Substring(pinfo.Address.Address.IndexOf(" ")+1).Trim() + "','" +
                                                                              pinfo.Address.City + "','" +
                                                                              pinfo.Address.State + "'," +
                                                                              int.Parse(pinfo.Address.ZipCode) + "," +
                                                                              new DateTime(int.Parse(pinfo.DateOfBirth.Year), int.Parse(pinfo.DateOfBirth.Month), int.Parse(pinfo.DateOfBirth.Day)).Date.Ticks + ",'" +
                                                                              pinfo.ContactNumber.AreaCode+pinfo.ContactNumber.LocalCode+pinfo.ContactNumber.ResidentialCode + "');", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect == 0 ? false : true;
        }

        /**
        public int AddPatientRecord(string firstName, string lastName, DateTime dateOfBirth)
        {
            caufingDbConnection.Open();

            OdbcCommand maxId = new OdbcCommand("SELECT MAX(id) FROM PATIENT", caufingDbConnection);            
            int theMax = (int)maxId.ExecuteScalar();
            
            string newId = Convert.ToString(Convert.ToInt32(theMax+1));
            OdbcCommand comm = new OdbcCommand("INSERT INTO PATIENT VALUES('" + newId + "','" + firstName + "','" + lastName + "','" + "1" + "','" + "number one drive" + "','" + "Atlanta" + "','" + "Ga" + "','" + "30000" + "','" + dateOfBirth + "','" + "6781287034" + "');", caufingDbConnection);
            comm.ExecuteNonQuery(); 
                       
            caufingDbConnection.Close();
            return 1;
        }
        */

        public int UpdatePatientRecord(int PatientID, List<KeyValuePair<string, string>> updatedPatientData)
        {
            return 1;            
        }

        public List<KeyValuePair<string, object>>[] GetPatientRecord(int patientID)
        {
            return GetResults("SELECT * FROM Patient WHERE id=" + patientID + ";");
        }

        public int DeletePatientRecord(int patientID)
        {
            return 1;
            //return ExecuteNonQuery("");
        }

        public int AddLabRecord(int patientID, List<KeyValuePair<string, string>> labData)
        {
            throw new NotImplementedException();
        }

        public int UpdateLabRecord(int labID, List<KeyValuePair<string, string>> labData)
        {
            throw new NotImplementedException();
        }

        public List<KeyValuePair<string, string>> GetLabRecord(int labID)
        {
            throw new NotImplementedException();
        }

        public int GetEmployeeID(string firstName, string lastName)
        {
          //SELECT  `id` 
//FROM EMPLOYEE
//WHERE FirstName =  'Aaron'
//AND LastName =  'Schwan
            //return GetResults("SELECT `id` FROM EMPLOYEE WHERE FirstName='" + firstName + "'AND LastName='" + lastName + "';");
            return 1;
        }

        public bool Authenticate(int employeeID, string securePassword)
        {
         //   try
        //    {
                caufingDbConnection.Open();
                OdbcCommand comm = new OdbcCommand("SELECT eid, password FROM Login WHERE eid = " + employeeID + " AND password = '" + securePassword+"' ;", caufingDbConnection);
                OdbcDataReader reader;
                reader = comm.ExecuteReader();
                if (reader.Read())
                {
                    reader.Close();
                    caufingDbConnection.Close();
                    return true;
                }
                reader.Close();
                caufingDbConnection.Close();
          //  }
        //    catch (Exception e)
        //    {
         //       throw new Exception("An exception occured.", e);
               //Console.WriteLine(e.Message);
        //    }
            return false;
        }

        private List<KeyValuePair<string, object>>[] GetResults(string query)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand(query, caufingDbConnection);
            comm.ExecuteNonQuery();
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();

            List<KeyValuePair<string, object>>[] user = new List<KeyValuePair<string, object>>[MyDataReader.RecordsAffected];

            int count = 0;
            while (MyDataReader.Read())
            {
                List<KeyValuePair<string, object>> thisRecord = new List<KeyValuePair<string, object>>();
                for (int i = 0; i < MyDataReader.FieldCount; i++)
                    thisRecord.Add(new KeyValuePair<string, object>(MyDataReader.GetName(i), MyDataReader.GetValue(i)));
                user[count] = thisRecord;
                //  user[count] = (new KeyValuePair<string,string>(Convert.ToString(count),MyDataReader.GetString(count)), count);
                count++;
            }

            caufingDbConnection.Close();
            return user;
        }

        ////////////////////////////////////////////////////////////////////////////////
        // Expermential "Search" methods German name used to avoid method confusion
        ///////////////////////////////////////////////////////////////////////////////
        public PatientInformation.PatientRecord[] getPatientResult(string query)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand(query, caufingDbConnection);
            comm.ExecuteNonQuery();
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();
            
            PatientInformation.PatientRecord[] list = new PatientInformation.PatientRecord[MyDataReader.RecordsAffected];
            PatientInformation.PatientRecord patient;
            int count = 0;
            while (MyDataReader.Read())
            {
                patient = new PatientInformation.PatientRecord();

                // Gets the id, first and last name
                patient.SetName((int)MyDataReader.GetValue(0),(string)MyDataReader.GetValue(1),(string)MyDataReader.GetValue(3));
                // Gets the street address, city, state and zip code
                patient.SetAddress((int)MyDataReader.GetValue(4), (string)MyDataReader.GetValue(5), (string)MyDataReader.GetValue(6), (string)MyDataReader.GetValue(7), (int)MyDataReader.GetValue(8));
                // Gets the date of birth
                DateTime dob = new DateTime((long)MyDataReader.GetValue(9));
                patient.SetDateOfBirth(dob.Month, dob.Day, dob.Year);
                // Gets the contact number
                patient.SetContactNumber(int.Parse(((string)MyDataReader.GetValue(10)).Substring(0, 3)),
                                                int.Parse(((string)MyDataReader.GetValue(10)).Substring(3, 3)),
                                                int.Parse(((string)MyDataReader.GetValue(10)).Substring(6)));
                list[count] = patient;
                count++;
            }

            caufingDbConnection.Close();
            
            return list;
        }


        public PatientInformation.PatientRecord[] suchen(string firstName, string lastName)
        {
            return getPatientResult("SELECT * FROM `Patient` WHERE FirstName='" + firstName + "' AND LastName='" + lastName + "'");
        }

        public PatientInformation.PatientRecord[] suchen(DateTime dateOfBirth)
        {
            return getPatientResult("SELECT * FROM Patient WHERE DateofBirth=" + dateOfBirth.Ticks + ";");
        }

        public PatientInformation.PatientRecord[] suchen(string lastName, DateTime dateOfBirth)
        {
            return getPatientResult("SELECT * FROM Patient WHERE LastName='" + lastName + "' AND DateofBirth=" + dateOfBirth.Ticks + ";");
        }

        public PatientInformation.PatientRecord[] suchen(string firstName, string lastName, DateTime dateOfBirth)
        {
            return getPatientResult("SELECT * FROM Patient WHERE FirstName='" + firstName + "' AND LastName='" + lastName + "' AND DateofBirth=" + dateOfBirth.Ticks + ";");
        }

        public int updatePatientRecord(int PatientID, PatientInformation.PatientRecord updatedPatientData)
        {
            caufingDbConnection.Open();
            string firstName = updatedPatientData.Name.FirstName ;
            string lastName = updatedPatientData.Name.LastName;
            int streetnum = int.Parse(updatedPatientData.Address.Address.Substring(0, updatedPatientData.Address.Address.IndexOf(" ")).Trim());
            string streetname = updatedPatientData.Address.Address.Substring(updatedPatientData.Address.Address.IndexOf(" ") + 1).Trim();
            string city = updatedPatientData.Address.City;
            string state = updatedPatientData.Address.State;
            int zipcode = int.Parse(updatedPatientData.Address.ZipCode);

            OdbcCommand comm = new OdbcCommand("UPDATE Patient SET FirstName='" +
                                                                              firstName + "', LastName='" +
                                                                              lastName + "',`Street#`='" +
                                                                              streetnum + "', StreetName='" +
                                                                              streetname + "',City='" +
                                                                              city + "',`State`='" +
                                                                              state + "',ZipCode='" +
                                                                              zipcode + "', DateOfBirth='" +
                                                                              updatedPatientData.DateOfBirth + "',`Contact#`='" +
                                                                              (updatedPatientData.ContactNumber.AreaCode + updatedPatientData.ContactNumber.LocalCode + updatedPatientData.ContactNumber.ResidentialCode)
                                                                              + "' WHERE id='" + PatientID + "' ;", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public PatientInformation.PatientRecord[] getPatientRecord(int patientID)
        {
            return getPatientResult("SELECT * FROM Patient WHERE id='" + patientID + "';");
        }

        public int deletePatientRecord(int patientID)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("DELETE FROM Patient WHERE id='" + patientID +"';", caufingDbConnection);
            comm.ExecuteNonQuery();

            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public int addPatientVisitRecord(int nurseID, DateTime date, int patientID, PatientInformation.PatientRecord.PatientVisitRecord visit)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("INSERT INTO Visit(NurseID, VisitDate, PatientID) VALUES (" +
                                                                             nurseID + "," +
                                                                             visit.VisitDate.Ticks + "," +
                                                                             patientID + ");", caufingDbConnection);
            comm.ExecuteNonQuery();

           comm = new OdbcCommand("INSERT INTO Record(NurseID, VisitDateTime, PatientID, Symptoms, Temperature, Pulse, BloodPressureHDL, BloodPressureLDL, Diagnosis, DoctorID) VALUES ('" +
                                                                             nurseID + "','" +
                                                                             visit.VisitDate.Ticks + "'," +
                                                                             patientID + ",'" +
                                                                             visit.Symptoms + "','" +
                                                                             visit.Temperature + "','" +
                                                                             visit.Pulse + "','" +
                                                                             visit.BloodPressure1 + "','" +
                                                                             visit.BloodPressure2 + "','" +
                                                                             visit.Diagnosis + "'," +
                                                                             visit.DoctorID + ");", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public PatientInformation.PatientRecord.PatientVisitRecord[] getPatientVisitResult(string query)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand(query, caufingDbConnection);
            comm.ExecuteNonQuery();
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();

            PatientInformation.PatientRecord.PatientVisitRecord[] list = new PatientInformation.PatientRecord.PatientVisitRecord[MyDataReader.RecordsAffected];
            PatientInformation.PatientRecord.PatientVisitRecord visit;
            int count = 0;
            while (MyDataReader.Read())
            {
                visit = new PatientInformation.PatientRecord.PatientVisitRecord();

                visit.VisitDate = new DateTime((long)MyDataReader.GetValue(1));
                visit.VisitDateLong = (long)MyDataReader.GetValue(1);
                visit.Symptoms = (string)MyDataReader.GetValue(3);
                visit.Temperature = ((Double)MyDataReader.GetValue(4)).ToString();
                visit.Pulse = ((int)MyDataReader.GetValue(5)).ToString();
                visit.BloodPressure1 = ((int)MyDataReader.GetValue(6)).ToString();
                visit.BloodPressure2 = ((int)MyDataReader.GetValue(7)).ToString();
                visit.Diagnosis = MyDataReader.GetString(8);
                list[count] = visit;
                count++;
            }

            caufingDbConnection.Close();

            return list;
        }

        public PatientInformation.PatientRecord.PatientVisitRecord[] getPatientVisitRecord(int patientID)
        {
            return getPatientVisitResult("SELECT * FROM Record WHERE patientID='" + patientID + "';");
        }

        public PatientInformation.PatientRecord.PatientVisitRecord[] getPatientVisitRecord(int nurseID, int patientID)
        {
            return getPatientVisitResult("SELECT * FROM Record WHERE patientID='" + patientID + "' AND nurseID='" + nurseID + "';");
        }

        public int updatePatientVisitRecord(int PatientID, DateTime date, PatientInformation.PatientRecord.PatientVisitRecord updatedPatientVisitData)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("UPDATE Record SET Symptoms='" + updatedPatientVisitData.Symptoms +
                                                                "', Temperature='" + double.Parse(updatedPatientVisitData.Temperature) +
                                                                "', Pulse='" + int.Parse(updatedPatientVisitData.Pulse) +
                                                                "', BloodPressureHDL='" + int.Parse(updatedPatientVisitData.BloodPressure1) +
                                                                "', BloodPressureLDL='" + int.Parse(updatedPatientVisitData.BloodPressure2) +
                                                                "', Diagnosis='" + updatedPatientVisitData.Diagnosis +
                                                                "', DoctorID=" + updatedPatientVisitData.DoctorID +
                                                                " WHERE PatientID='" + PatientID + "' AND VisitDateTime=" + updatedPatientVisitData.VisitDateLong + ";", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public int deletePatientVisitRecord(int patientID, long date)
        {
            caufingDbConnection.Open();
          //  OdbcCommand comm = new OdbcCommand("DELETE FROM Record WHERE PatientID='" + patientID + "' AND VisitDateTime='" + date + "';", caufingDbConnection);
            OdbcCommand comm = new OdbcCommand("DELETE FROM Visit WHERE PatientID='" + patientID + "' AND VisitDate='" + date + "';", caufingDbConnection);
            int rowsaffect;
            try
            {
                rowsaffect = comm.ExecuteNonQuery();
            }
            catch (System.InvalidOperationException)
            {
                return 0;
            }

            //int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public int addPatientLabRecord(int nurseID, int patientID, int doctorID, long date, int type, int result)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("INSERT INTO LabTest(NurseID, PatientID, DoctorID, VisitDateTime,TypeID, Result) VALUES ('" +
                                                                             nurseID + "','" +
                                                                             patientID + "','" +
                                                                             doctorID + "','" +
                                                                             date + "','" +
                                                                             type + "','" +
                                                                             result + "');", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public PatientInformation.PatientRecord.PatientLabRecord[] getPatientLabRecord(int nurseID, int patientID)
        {
            //return getPatientLabResult("SELECT * FROM Record WHERE patientID='" + patientID + "'AND nurseID='" + nurseID + "';");
            return getPatientLabResult("SELECT LabTest.VisitDateTime, LabTest.Result, AvailableTests.Name FROM LabTest, AvailableTests WHERE LabTest.patientID='" + patientID + "' AND LabTest.TypeID=AvailableTests.id;");
        }

        public PatientInformation.PatientRecord.PatientLabRecord[] getPatientLabRecord(long visitDate, int patientID)
        {
            //return getPatientLabResult("SELECT * FROM Record WHERE patientID='" + patientID + "'AND nurseID='" + nurseID + "';");
            return getPatientLabResult("SELECT LabTest.VisitDateTime, LabTest.Result, AvailableTests.Name FROM LabTest, AvailableTests WHERE LabTest.patientID='" + patientID + "' AND LabTest.VisitDateTime = " + visitDate + " AND LabTest.TypeID=AvailableTests.id;");
        }

        //TODO: remove
      /**  public PatientInformation.PatientRecord.PatientLabRecord[] getPatientLabRecord(DateTime date, int patientID)
        {
            return getPatientLabResult("SELECT * FROM Record WHERE patientID='" + patientID + "'AND VisitDateTime='" + date + "';");
        }**/
        
        public PatientInformation.PatientRecord.PatientLabRecord[] getPatientLabRecord(DateTime date, int patientID, int doctorID)
        {
            return getPatientLabResult("SELECT * FROM Record WHERE patientID='" + patientID + "'AND VisitDateTime='" + date + "'AND doctorID='" + doctorID + "';");
        }

        public PatientInformation.PatientRecord.PatientLabRecord[] getPatientLabResult(string query)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand(query, caufingDbConnection);
            comm.ExecuteNonQuery();
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();

            PatientInformation.PatientRecord.PatientLabRecord[] list = new PatientInformation.PatientRecord.PatientLabRecord[MyDataReader.RecordsAffected];
            PatientInformation.PatientRecord.PatientLabRecord visit;
            int count = 0;
            while (MyDataReader.Read())
            {
                visit = new PatientInformation.PatientRecord.PatientLabRecord();

                visit.VisitDate = new DateTime((long)MyDataReader.GetValue(0));
                visit.VisitDateLong = ((long)MyDataReader.GetValue(0));
                visit.Type = (string)MyDataReader.GetValue(2);
                visit.Result = (int)MyDataReader.GetValue(1) == 0 ? "negative" : "positive";

                list[count] = visit;
                count++;
            }

            caufingDbConnection.Close();

            return list;
        }


        public int updatePatientLabRecord(int nurseID, int patientID, int doctorID, long date, int type, int result)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("UPDATE LabTest SET TypeID='" + type +
                                                                              "', Result='" + result + "' " +
                                                                              "WHERE VisitDateTime="+ date +
                                                                              " AND NurseID=" + nurseID +
                                                                              " AND TypeID=" + type + ";", caufingDbConnection);
            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public int deletePatientLabRecord(int patientID, DateTime date)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("DELETE FROM LabTest WHERE PatientID='" + patientID + "' AND VisitDateTime='" + date + "';", caufingDbConnection);
           // comm.ExecuteNonQuery();

            int rowsaffect = comm.ExecuteNonQuery();
            caufingDbConnection.Close();

            return rowsaffect;
        }

        public LabTestInformation[] GetAvailableTests()
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("SELECT * FROM AvailableTests", caufingDbConnection);
            comm.ExecuteNonQuery();
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();
            
            LabTestInformation[] results = new LabTestInformation[MyDataReader.RecordsAffected];
            int count = 0;
            while (MyDataReader.Read())
            {
                int id = MyDataReader.GetInt32(0);
                string name = MyDataReader.GetString(1);
                LabTestInformation test = new LabTestInformation(id, name);
                results[count++] = test;
            }
            caufingDbConnection.Close();
            return results;
        }

        public DoctorInformation[] GetDoctorInformation()
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("SELECT Employee.id, Employee.FirstName, Employee.LastName FROM Employee, Doctor WHERE Employee.id = Doctor.eid", caufingDbConnection);
            OdbcDataReader MyDataReader;
            MyDataReader = comm.ExecuteReader();

            DoctorInformation[] results = new DoctorInformation[MyDataReader.RecordsAffected];

            int count = 0;
            while (MyDataReader.Read())
            {
                int id = MyDataReader.GetInt32(0);
                string name = MyDataReader.GetString(1) + " " + MyDataReader.GetString(2);
                DoctorInformation test = new DoctorInformation(id, name);
                results[count++] = test;
            }
            caufingDbConnection.Close();
            return results;

        }

        public System.Data.DataTable AdminQuery(string query)
        {
            
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand(query, caufingDbConnection);
            OdbcDataReader MyDataReader;
            try
            {
                
                MyDataReader = comm.ExecuteReader();
            }
            catch (OdbcException e)
            {
                caufingDbConnection.Close();
                throw new ArgumentException("An error occured while querying the database.", e);
            }


            int fieldCount = MyDataReader.VisibleFieldCount;
            System.Data.DataTable dataTable = new System.Data.DataTable();
            int j = 1;
            for (int i = 0; i < fieldCount; i++)
            {
                System.Data.DataColumn column = new System.Data.DataColumn(MyDataReader.GetName(i), MyDataReader.GetFieldType(i));
                column.Caption = column.ColumnName;
                try
                {
                    dataTable.Columns.Add(column);
                }
                catch (System.Data.DuplicateNameException)
                {
                    column.ColumnName = column.ColumnName + j++;
                    dataTable.Columns.Add(column);
                }
            }

            while (MyDataReader.Read())
            {
                object[] array = new object[MyDataReader.FieldCount];
                MyDataReader.GetValues(array);
                dataTable.Rows.Add(array);
            }
            caufingDbConnection.Close();
            return dataTable;
            
        }

        public bool isAdmin(int loginID)
        {
            caufingDbConnection.Open();
            OdbcCommand comm = new OdbcCommand("SELECT *  FROM Admin WHERE Admin.eid=" + loginID + ";", caufingDbConnection);
            bool result = comm.ExecuteNonQuery() == 0 ? false : true;
            caufingDbConnection.Close();
            return result;
        }
        // TODO 
        // test all experimental methods - mustly done
        // extract new method from delete and update methods - not possible- you MUST BELIEVE!!
        

        //END
        ////////////////////////////////////////////////////////////////////////////////////
    }
}
